//
//  MultipleNewsCell.m
//  SouthChina
//
//  Created by creator_lq on 14/12/25.
//  Copyright (c) 2014年 科创. All rights reserved.
//

#import "MultipleNewsCell.h"

@implementation MultipleNewsCell


@synthesize message;
@synthesize publicId;



- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    
    if (self) {
        
        _allcontentView =[[UIImageView alloc]initWithFrame:CGRectZero];
        _backgroundImage =[[UIImageView alloc]initWithFrame:CGRectZero];

        _bigContentLabel =[[UILabel alloc]initWithFrame:CGRectZero];
        [_bigContentLabel setFont:[UIFont fontWithName:@"TrebuchetMS-Bold" size:14]];
 
        _bigGroundImage =[[UIImageView alloc]initWithFrame:CGRectZero];
        
        [_bigGroundImage addSubview:_bigContentLabel];
        [_backgroundImage addSubview:_bigGroundImage];
        
        [self addSubview:_backgroundImage];

        [self setBackgroundColor:[UIColor clearColor]];
    
    }
    return self;
}


- (void)layoutSubviews{
    NSDictionary * allmap =[message getValue:@"news"];
    NSArray * artlist = [allmap objectForKey:@"Articles"];
    NSDictionary *fistMap = [artlist objectAtIndex:0];

    long totalHeight = BIG_IMAGE_HEIGHT +3*NEW_CELL_COM_GAP +(artlist.count -1)*CELL_OTHER_CELL;
        
    self.allcontentView.frame =  CGRectMake(0, 0, SCREEN_WIDTH, totalHeight);
    self.backgroundImage.frame =  CGRectMake(NEW_CELL_COM_GAP, NEW_CELL_COM_GAP, SCREEN_WIDTH-2*NEW_CELL_COM_GAP, totalHeight-2*NEW_CELL_COM_GAP);
    [self.backgroundImage setImage:[[UIImage imageNamed:@"news_background_normal"]stretchableImageWithLeftCapWidth:5 topCapHeight:5]];
    
    UIButton *fisrBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    fisrBtn.frame = CGRectMake(NEW_CELL_COM_GAP, NEW_CELL_COM_GAP, self.backgroundImage.frame.size.width-2*NEW_CELL_COM_GAP, BIG_IMAGE_HEIGHT);
    [fisrBtn addTarget:self action:@selector(onButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    fisrBtn.tag = 0;
    [self addSubview:fisrBtn];
    
    
    _bigGroundImage.frame =CGRectMake(NEW_CELL_COM_GAP, NEW_CELL_COM_GAP, self.backgroundImage.frame.size.width-2*NEW_CELL_COM_GAP, BIG_IMAGE_HEIGHT);
    NSString * url =[fistMap objectForKey:@"PicUrl"];
    NSURL *urll = [NSURL URLWithString: [url hasPrefix:@"http"]?url:[NSString stringWithFormat:@"%@%@",DE.fileEngineeUrl,url]];
    [self.bigGroundImage setImageWithURL:urll  placeholderImage:[UIImage imageNamed:@"empty_photo"]];

    
    _bigContentLabel.frame =CGRectMake(0,BIG_IMAGE_HEIGHT-BIG_TITLE_HEIGHT, self.bigGroundImage.frame.size.width, BIG_TITLE_HEIGHT);
    self.bigContentLabel.text = [fistMap objectForKey:@"Title"];
    _bigContentLabel.backgroundColor=[UIColor colorWithRed:119/255.0f green:136/255.0f blue:153/255.0f alpha:0.8];

    if (artlist && artlist.count>1) {
        
        
        for (int i =1; i<artlist.count; i++) {
            
            NSDictionary *newsMap = [artlist objectAtIndex:i];

            UIView *cellView = [[UIView alloc] initWithFrame:CGRectMake(NEW_CELL_COM_GAP, NEW_CELL_COM_GAP+BIG_IMAGE_HEIGHT+(i-1)*CELL_OTHER_CELL,self.backgroundImage.frame.size.width-2*NEW_CELL_COM_GAP, CELL_OTHER_CELL)];
            
            UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, 1,cellView.frame.size.width, 1)];
            [line setBackgroundColor:BASIC_GRAYLINE_COLOR];
            UILabel *textLable = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, cellView.frame.size.width-50, CELL_OTHER_CELL)];

            textLable.numberOfLines =2;
            [textLable setFont:[UIFont systemFontOfSize:14]];
            textLable.text = [newsMap objectForKey:@"Title"];
            
            UIImageView *cellImage = [[UIImageView alloc] initWithFrame:CGRectMake(cellView.frame.size.width-50, 6, 50,50)];
            cellImage.layer.masksToBounds = YES;
            cellImage.layer.cornerRadius = 6.0;
            NSString * url =[newsMap objectForKey:@"PicUrl"];
            NSURL *urll = [NSURL URLWithString: [url hasPrefix:@"http"]?url:[NSString stringWithFormat:@"%@%@",DE.fileEngineeUrl,url]];
            [cellImage setImageWithURL:urll  placeholderImage:[UIImage imageNamed:@"empty_photo"]];

            [_backgroundImage addSubview:cellView];
            [cellView addSubview:line];
            [cellView addSubview:textLable];
            [cellView addSubview:cellImage];
            
            
            UIButton *fisrBtn = [UIButton buttonWithType:UIButtonTypeCustom];
            fisrBtn.frame = cellView.frame;
            [fisrBtn addTarget:self action:@selector(onButtonClick:) forControlEvents:UIControlEventTouchUpInside];
            fisrBtn.tag = i;
            [self addSubview:fisrBtn];
            
        }
     
    }

}

-(void) setMsgCellValue:(Message*)msg publicId:(NSString*)publicIdd{
    
    self.message = msg;
    self.publicId = publicIdd;
    
}

-(void)onButtonClick:(UIView*)view{
    
    NSDictionary * allmap =[message getValue:@"news"];
    NSArray * artlist = [allmap objectForKey:@"Articles"];
 
    NSDictionary *clickData = [artlist objectAtIndex:view.tag];
    WebViewController * detail = [[WebViewController alloc] init];
    detail.webUrl = [clickData objectForKey:@"Url" ];
    detail.publicId = publicId;
    [kAppDelegate.iCSDrawerController.navigationController pushViewController:detail animated:NO];
}



@end
